package com.hx.controller;

import com.hx.dto.MoodDTO;
import com.hx.service.MoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/mood")
@Slf4j
public class MoodController {

    @Resource
    private MoodService moodService;

    @RequestMapping
    public @ResponseBody String index(){
        return "欢迎访问 MoodController...";
    }

    /**
     * 查询所有说说列表，并返回”mood“页面
     * @param model
     * @return
     */
    @GetMapping("/findAllMoods")
    public String findAllMoods(Model model){

        List<MoodDTO> allMoods = moodService.findAllMoods();
        model.addAttribute("moods",allMoods);
        return "mood";
    }

    /**
     * 点赞  http://localhost:8080/{moodId}/praise?userId=xxx
     * @param model
     * @param moodId
     * @param userId
     * @return
     */
    @GetMapping("/{moodId}/praise")
    public String praiseMood(Model model,
                             @PathVariable("moodId") String moodId,
                             @RequestParam("userId") String userId){

        boolean isPraise = moodService.praiseMood(userId, moodId);
        System.out.println("isPraise:"+isPraise);

        List<MoodDTO> allMoods = moodService.findAllMoods();
        model.addAttribute("moods",allMoods);
        model.addAttribute("isPraise",isPraise);
        return "mood";

    }

    /**
     * 点赞  http://localhost:8080/{moodId}/praiseByRedis?userId=xxx
     * redis + mysql
     * @param model
     * @param moodId
     * @param userId
     * @return
     */
    @GetMapping("/{moodId}/praiseByRedis")
    public String praiseMoodByRedis(Model model,
                             @PathVariable("moodId") String moodId,
                             @RequestParam("userId") String userId){

        //随机生成用户id(模拟)
        userId = new Random().nextInt(100) + "";

        //点赞 缓存在redis的Set集合中 实现幂等性
        boolean isPraise = moodService.praiseMoodByRedis(userId, moodId);
        System.out.println("isPraise:"+isPraise);

        //查询所有说说记录  mysql已存 + redis缓存
        List<MoodDTO> allMoods = moodService.findAllMoodsByRedis();
        model.addAttribute("moods",allMoods);
        model.addAttribute("isPraise",isPraise);
        return "mood";

    }


    /**
     * 点赞  http://localhost:8080/{moodId}/praiseByMQ?userId=xxx
     * redis + mysql +MQ
     * @param model
     * @param moodId
     * @param userId
     * @return
     */
    @GetMapping("/{moodId}/praiseByMQ")
    public String praiseMoodByMQ(Model model,
                                    @PathVariable("moodId") String moodId,
                                    @RequestParam("userId") String userId){

        //随机生成用户id(模拟)
        userId = new Random().nextInt(100) + "";

        //点赞 缓存在redis的Set集合中 实现幂等性
        boolean isPraise = moodService.praiseMoodByMQ(userId, moodId);
        System.out.println("isPraise:"+isPraise);

        //查询所有说说记录  mysql已存 + redis缓存
        List<MoodDTO> allMoods = moodService.findAllMoodsByRedis();
        model.addAttribute("moods",allMoods);
        model.addAttribute("isPraise",isPraise);
        return "mood";

    }
}
